﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.dao;

namespace BOM.BaseData
{
    public partial class MaterialList : Form
    {
        public MaterialList()
        {
            InitializeComponent();
        }

        public static int JieDianID = -1;//标记单击节点时的fid
        public static int JieDianID1 = -1;
        public static int XinZenggroupid = -1; //标记当点击新增时 记下此时的ID
        //true表示不选中 false 代表选中
        public static bool Subordinatebtn = true; //记下是否单击chekbox

        //新增按钮
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            MaterialForm form = new MaterialForm();
            form.Owner = this;
            CustomerList.BiaoJi = 2;
            form.ShowDialog();
        }

        //加载窗体时 调用此函数加载节点
        public void JiaZaiJieDian()
        {
            TreeNode Tou = new TreeNode("材料分类"); //创建个材料分类的节点
            treeView1.Nodes.Add(Tou);
            Tou.Tag = 0;
            //调用函数查找表materialgroup的值
            DataTable table = MaterialNew.materialgroupChaXun();
            if (table != null)
            {
                //MessageBox.Show(table.Rows.Count.ToString());
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow row = table.Rows[i];
                    if ((int)row["fparentid"] == 0)
                    {
                        TreeNode DiEr = new TreeNode("" + (string)row["fname"] + "");
                        DiEr.Tag = row["fid"];
                        Tou.Nodes.Add(DiEr);
                        //根据添加的节点 查看当前节点 是否有子节点
                        DataTable tables = MaterialNew.BoollZiJieDian((int)row["fid"]);
                        if (tables.Rows.Count != 0)
                        {
                            //有子节点就赋值为 -1 并添加 + 号
                            DiEr.Nodes.Add("-1");
                        }
                    }
                }
            }
        }
        //清除 treeview上的节点 和更新datagridview的数据
        public void JieDianQingChu()
        {
            treeView1.Nodes.Clear();
        }
        //根据 ID更新datagridview数据
        public void GengXindatagridview()
        {
            try
            {
                DataTable table;
                if (JieDianID1 != 0)
                {
                    table = MaterialNew.MaterialandMargroupIDCX(JieDianID1);
                }
                else
                {
                    table = MaterialNew.materialChaXun();
                }
                if (table != null)
                {
                    dataGridView1.DataSource = table;
                }
                else
                {
                    dataGridView1.DataSource = table;
                    //MessageBox.Show("没有查询到记录", "提示");
                }
            }

            catch
            { }
        }

        //窗体加载事件
        private void MaterialList_Load(object sender, EventArgs e)
        {
            //设定dataGridView1的列名只能是指定列名
            dataGridView1.AutoGenerateColumns = false; 
            JiaZaiJieDian();
        }
        //展开节点时 触发的事件
        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text == "-1")
            {
                //清除临时节点 -1
                e.Node.Nodes.Clear();
                int fid = (int)e.Node.Tag;
                //MessageBox.Show(fid.ToString());
                DataTable table = MaterialNew.BoollZiJieDian(fid);
                if (table.Rows.Count != 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        DataRow row = table.Rows[i];
                        TreeNode DiEr = new TreeNode("" + (string)row["fname"] + "");
                        DiEr.Tag = row["fid"];
                        //把满足条件子节点添加到当前节点中
                        e.Node.Nodes.Add(DiEr);
                        //根据添加的节点 查看当前节点 是否有子节点
                        DataTable tables = MaterialNew.BoollZiJieDian((int)row["fid"]);
                        if (tables.Rows.Count != 0)
                        {
                            DiEr.Nodes.Add("-1");
                        }
                    }
                }
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            //MessageBox.Show(e.Node.Tag.ToString());
        }

        private void treeView1_Click(object sender, EventArgs e)
        {
            

            //MessageBox.Show(e.Node.Tag.ToString());
        }

        //单击某个节点时发生
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                //得到单击时fid 根据这个fid查询material表中的值 显示在右边
                JieDianID = (int)treeView1.SelectedNode.Tag;
                //MessageBox.Show(JieDianID.ToString());
                if (Subordinatebtn == true)
                {
                    DataTable table = new DataTable();
                    //判断是否点击材料分类
                    if (JieDianID != 0)
                    {
                        table = MaterialNew.MaterialandMargroupIDCX(JieDianID);
                    }
                    else
                    {
                        //table = MaterialNew.materialChaXun();
                    }
                    if (table != null)
                    {
                        dataGridView1.DataSource = table;
                    }
                    else
                    {
                        dataGridView1.DataSource = table;
                        //MessageBox.Show("没有查询到记录", "提示");
                    }
                }
                else if (Subordinatebtn == false)
                {
                    DataTable table = new DataTable();
                    //判断是否点击材料分类
                    if (JieDianID != 0)
                    {
                        table = MaterialNew.SubordinateMaterial(JieDianID);
                    }
                    else
                    {
                        //table = MaterialNew.materialChaXun();
                    }
                    if (table != null)
                    {
                        dataGridView1.DataSource = table;
                    }
                    else
                    {
                        dataGridView1.DataSource = table;
                        //MessageBox.Show("没有查询到记录", "提示");
                    }
                }

            }
            catch
            { }
            JieDianID1 = (int)treeView1.SelectedNode.Tag;
        }
        //查看按钮
        private void btnView_Click(object sender, EventArgs e)
        {
                try
                {
                    CustomerList.XuanZID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["ID"].Value);
                    MaterialForm form = new MaterialForm();
                    form.Owner = this;
                    CustomerList.BiaoJi = 0;
                    form.Show();
                }
                catch
                {
                }
        }
        //修改按钮
        private void btnEdit_Click(object sender, EventArgs e)
        {
                try
                {
                    CustomerList.XuanZID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["ID"].Value);
                    MaterialForm form = new MaterialForm();
                    form.Owner = this;
                    CustomerList.BiaoJi = 1;
                    form.Show();
                }
                catch
                { }
        }
        //保存按钮
        private void btnSave_Click(object sender, EventArgs e)
        {

        }
        //刷新按钮
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            JieDianQingChu();
            JiaZaiJieDian();
            GengXindatagridview();
        }
        //退出按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //删除按钮
        private void btnRemove_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("您确定要删除吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                try
                {
                    CustomerList.XuanZID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["ID"].Value);
                    bool bol = MaterialNew.DeleteMaterial(CustomerList.XuanZID);
                    if (bol == true)
                    {
                        GengXindatagridview();
                        MessageBox.Show("删除成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示");
                    }
                }
                catch
                {
                    MessageBox.Show("删除失败", "提示");
                }
            }
        }
        //下面的新增按钮
        private void btnAddNode_Click(object sender, EventArgs e)
        {
            if (JieDianID == -1)
            {
                MessageBox.Show("请选择需要添加材料的类型", "提示");
            }
            else
            {
                MaterialFenLei material = new MaterialFenLei();
                CustomerList.BiaoJi = 2; //新增
                material.Owner = this;
                material.ShowDialog();
            }

        }
        //下面的修改按钮
        private void btnEditNode_Click(object sender, EventArgs e)
        {
            if (JieDianID == 0 || JieDianID == -1)
            {
                MessageBox.Show("请选择需要修改材料的类型", "提示");
            }
            else
            {
                MaterialFenLei material = new MaterialFenLei();
                CustomerList.BiaoJi = 1; //修改
                material.Owner = this;
                material.ShowDialog();
            }
        }
        //下面的删除按钮
        private void btnRemoveNode_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("您确定要删除吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                try
                {
                    bool bol = MaterialgroupNew.DeleteFidMaterialgroup(MaterialList.JieDianID);
                    bool bol1 = MaterialgroupNew.DeleteFparentidMaterialgroup(MaterialList.JieDianID);
                    if (bol == true)
                    {
                        JieDianQingChu();
                        JiaZaiJieDian();
                        GengXindatagridview();
                        MessageBox.Show("删除成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示");
                    }
                }
                catch(Exception e3)
                {
                    MessageBox.Show(e3.Message);
                }
            }
        }

        private void MaterialList_Resize(object sender, EventArgs e)
        {

        }
        //双击行发生的事件 打开查看内容
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                CustomerList.XuanZID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["ID"].Value);
                MaterialForm form = new MaterialForm();
                form.Owner = this;
                CustomerList.BiaoJi = 0;
                form.Show();
            }
            catch
            {
            }
        }
        //每当checkbox值发生改变时 发生这个事件
        private void isSub_CheckedChanged(object sender, EventArgs e)
        {
            if (Subordinatebtn == true)
            {
                Subordinatebtn = false; //代表选中
            }
            else
            {
                Subordinatebtn = true; //代表不选中
            }
            //单击过后立即发生变化
            try
            {
                //得到单击时fid 根据这个fid查询material表中的值 显示在右边
                JieDianID = (int)treeView1.SelectedNode.Tag;
                //MessageBox.Show(JieDianID.ToString());
                if (Subordinatebtn == true)
                {
                    DataTable table = new DataTable();
                    //判断是否点击材料分类
                    if (JieDianID != 0)
                    {
                        table = MaterialNew.MaterialandMargroupIDCX(JieDianID);
                    }
                    else
                    {
                        //table = MaterialNew.materialChaXun();
                    }
                    if (table != null)
                    {
                        dataGridView1.DataSource = table;
                    }
                    else
                    {
                        dataGridView1.DataSource = table;
                        //MessageBox.Show("没有查询到记录", "提示");
                    }
                }
                else if (Subordinatebtn == false)
                {
                    DataTable table = new DataTable();
                    //判断是否点击材料分类
                    if (JieDianID != 0)
                    {
                        table = MaterialNew.SubordinateMaterial(JieDianID);
                    }
                    else
                    {
                        //table = MaterialNew.materialChaXun();
                    }
                    if (table != null)
                    {
                        dataGridView1.DataSource = table;
                    }
                    else
                    {
                        dataGridView1.DataSource = table;
                        //MessageBox.Show("没有查询到记录", "提示");
                    }
                }

            }
            catch
            { }
            JieDianID1 = (int)treeView1.SelectedNode.Tag;
            


        }
    }
}
